%% segmentXAxis
%
% Segments out the region with max eccentricity (>= 0.9999)
%
%% Synatax
%
% [ horizontalLines ] = segmentXAxis( rp )
%
%% Description
%
% segmentXAxis segments the result of a region properties call on an 
% image based on eccentricity. The goal of this function is to detect the
% straightest line in a processed image (eccentricity closest to 1, with minimum 
% eccentricity of 0.9999). 
%
%% Arguments
%
%* rp                       - Struct: The output of a regionprops() call on
%                                     an image
%                                           
%% Returns
%
%* horizontalLines          - Struct: The elements of the output of a regionprops() 
%                                     call on an image containing the highest 
%                                     eccentricty
%
%% Function Side Effects
%* N/A
%
%% Exceptions
%
%* An error is thrown if rp is empty or is of a type other than struct or
%
%* An error is thrown if rp does not contain the field name 'Eccentricity'
%
%% See Also
% regionprops
% dicomAnalysis
%

function [ horizontalLines ] = segmentXAxis( rp )


iP = inputParser;
iP.FunctionName = 'segmentXAxis';

% Checks whether passed in arguemnts are the correct type and not empty
rpValidationFcn = @(x) isstruct(x) && isempty(x) == 0 && isfield(rp, 'Eccentricity');

iP.addRequired('rp', rpValidationFcn);

try
    
    iP.parse(rp);
    
catch ME
    
    errorMsg = ['rp must be formatted as type struct, must not be empty,\n', ...
                'and must contain the field "Eccentricity"'];
    
    error('segmentXAxis:ImproperInput', errorMsg);
    
end

% Segments out the region with the maximum eccentricity >= 0.9999
horizontalLines = rp([rp.Eccentricity] > 0.9999);

if length(horizontalLines) > 1
    
    [maxValue, idx] = max([horizontalLines.Eccentricity]);
    
    horizontalLines = horizontalLines(idx);

end

end